/**************************************************************
 *@ProjectName: blweibo
 *@FileName: Topic.java
 *@Date: 2011-5-30
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo.models;

import java.io.Serializable;

import org.json.JSONObject;

import android.text.TextUtils;

import com.zhiweibo.util.TopicUtil;

/**
 * The Class Topic.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-30
 */
public class Topic implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -7889020064527641378L;

    /** The dateline. */
    private String dateline = "0";

    /** The tid. */
    private String tid = "";

    /** The text. */
    private String text = "";

    /** The from. */
    private String from = "";

    /** The favorited. */
    private Boolean favorited = false;

    /** The totid. */
    private String totid = "";

    /** The touid. */
    private String touid = "";

    /** The tousername. */
    private String tousername = "";

    /** The pic_s. */
    private String pic_s = "";

    /** The pic_o. */
    private String pic_o = "";

    /** The pic_a. */
    private String pic_a = "";

    /** The user. */
    private User user;

    /** The retopic. */
    private Topic retopic;

    /**
     * Instantiates a new topic.
     */
    public Topic() {
    }

    /**
     * Instantiates a new topic.
     * 
     * @param js
     *            the js
     */
    public Topic(JSONObject js) {
        if (js != null) {
            dateline = js.optString("dateline");
            tid = js.optString("tid");
            text = js.optString("text");
            from = js.optString("from");
            favorited = js.optBoolean("favorited");
            totid = js.optString("totid");
            touid = js.optString("touid");
            tousername = js.optString("tousername");
            pic_s = js.optString("pic_s");
            pic_o = js.optString("pic_o");
            pic_a = js.optString("pic_a");
            user = new User(js.optJSONObject("user"));
            retopic = new Topic();
            if (js.optJSONObject("retopic") != null) {
                JSONObject jsob = js.optJSONObject("retopic");
                retopic.dateline = jsob.optString("dateline");
                retopic.tid = jsob.optString("tid");
                retopic.text = jsob.optString("text");
                retopic.from = jsob.optString("from");
                retopic.favorited = jsob.optBoolean("favorited");
                retopic.totid = jsob.optString("totid");
                retopic.touid = jsob.optString("touid");
                retopic.tousername = jsob.optString("tousername");
                retopic.pic_s = jsob.optString("pic_s");
                retopic.pic_o = jsob.optString("pic_o");
                retopic.pic_a = jsob.optString("pic_a");
                retopic.user = new User(jsob.optJSONObject("user"));
                retopic.retopic = new Topic();
            }
        }
    }

    /**
     * Gets the dateline.
     * 
     * @return the dateline
     */
    public String getDateline() {
        return TopicUtil.formatDate(dateline);
    }

    /**
     * Gets the r dateline.
     * 
     * @return the r dateline
     */
    public String getRDateline() {
        return dateline;
    }

    /**
     * Sets the dateline.
     * 
     * @param dateline
     *            the new dateline
     */
    public void setDateline(String dateline) {
        this.dateline = dateline;
    }

    /**
     * Gets the tid.
     * 
     * @return the tid
     */
    public String getTid() {
        return tid;
    }

    /**
     * Sets the tid.
     * 
     * @param tid
     *            the new tid
     */
    public void setTid(String tid) {
        this.tid = tid;
    }

    /**
     * Gets the text.
     * 
     * @return the text
     */
    public String getText() {
        return text.replaceAll("[TI]_ADD.*_[TI]_ADD", "");
    }

    /**
     * Gets the h text.
     * 
     * @return the h text
     */
    public String getHText() {
        return text.replaceAll("[TI]_ADD.*_[TI]_ADD", "").replaceAll("<a.*>(.*)</a>", "$1");
    }

    /**
     * Sets the text.
     * 
     * @param text
     *            the new text
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     * Gets the from.
     * 
     * @return the from
     */
    public String getFrom() {
        return from;
    }

    /**
     * Sets the from.
     * 
     * @param from
     *            the new from
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * Gets the favorited.
     * 
     * @return the favorited
     */
    public Boolean getFavorited() {
        return favorited;
    }

    /**
     * Sets the favorited.
     * 
     * @param favorited
     *            the new favorited
     */
    public void setFavorited(Boolean favorited) {
        this.favorited = favorited;
    }

    /**
     * Gets the totid.
     * 
     * @return the totid
     */
    public String getTotid() {
        return totid;
    }

    /**
     * Sets the totid.
     * 
     * @param totid
     *            the new totid
     */
    public void setTotid(String totid) {
        this.totid = totid;
    }

    /**
     * Gets the touid.
     * 
     * @return the touid
     */
    public String getTouid() {
        return touid;
    }

    /**
     * Sets the touid.
     * 
     * @param touid
     *            the new touid
     */
    public void setTouid(String touid) {
        this.touid = touid;
    }

    /**
     * Gets the tousername.
     * 
     * @return the tousername
     */
    public String getTousername() {
        return tousername;
    }

    /**
     * Sets the tousername.
     * 
     * @param tousername
     *            the new tousername
     */
    public void setTousername(String tousername) {
        this.tousername = tousername;
    }

    /**
     * Gets the pic_s.
     * 
     * @return the pic_s
     */
    public String getPic_s() {
        return pic_s;
    }

    /**
     * Sets the pic_s.
     * 
     * @param pic_s
     *            the new pic_s
     */
    public void setPic_s(String pic_s) {
        this.pic_s = pic_s;
    }

    /**
     * Gets the pic_o.
     * 
     * @return the pic_o
     */
    public String getPic_o() {
        return pic_o;
    }

    /**
     * Sets the pic_o.
     * 
     * @param pic_o
     *            the new pic_o
     */
    public void setPic_o(String pic_o) {
        this.pic_o = pic_o;
    }

    /**
     * Gets the pic_a.
     * 
     * @return the pic_a
     */
    public String getPic_a() {
        return pic_a;
    }

    /**
     * Sets the pic_a.
     * 
     * @param pic_a
     *            the new pic_a
     */
    public void setPic_a(String pic_a) {
        this.pic_a = pic_a;
    }

    /**
     * Gets the user.
     * 
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * Sets the user.
     * 
     * @param user
     *            the new user
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * Gets the retopic.
     * 
     * @return the retopic
     */
    public Topic getRetopic() {
        return retopic;
    }

    /**
     * Sets the retopic.
     * 
     * @param retopic
     *            the new retopic
     */
    public void setRetopic(Topic retopic) {
        this.retopic = retopic;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Topic [dateline=" + dateline + ", tid=" + tid + ", text=" + text + ", from=" + from + ", favorited="
                + favorited + ", totid=" + totid + ", touid=" + touid + ", tousername=" + tousername + ", pic_s="
                + pic_s + ", pic_o=" + pic_o + ", pic_a=" + pic_a + ", user=" + user + ", retopic=" + retopic + "]";
    }

    /**
     * To json string.
     * 
     * @return the string
     */
    public String toJSONString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"dateline\"").append(":\"").append(dateline).append("\",");
        sb.append("\"tid\"").append(":\"").append(tid).append("\",");
        sb.append("\"text\"").append(":\"").append(text).append("\",");
        sb.append("\"from\"").append(":\"").append(TopicUtil.formatFrom(from)).append("\",");
        sb.append("\"totid\"").append(":\"").append(totid).append("\",");
        sb.append("\"touid\"").append(":\"").append(touid).append("\",");
        sb.append("\"tousername\"").append(":\"").append(tousername).append("\",");
        sb.append("\"pic_s\"").append(":\"").append(pic_s).append("\",");
        sb.append("\"pic_o\"").append(":\"").append(pic_o).append("\",");
        sb.append("\"pic_a\"").append(":\"").append(pic_a).append("\",");
        sb.append("\"user\"").append(":").append(user.toJSONString());
        if (!TextUtils.isEmpty(retopic.getTid())) {
            sb.append(",").append("\"retopic\"").append(":").append(retopic.toJSONString());
        }
        sb.append("}");
        return sb.toString();
    }

}
